package cn.lizheng.dao;

import cn.lizheng.DBUtil.DBUtil;
import cn.lizheng.pojo.Admininfo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdmininfoDAOImpl implements AdmininfoDAO {
    @Override
    public int count() {
        int total = 0;
        try {
            Connection conn = DBUtil.getConn();
            String sql = "select count(*) as nums from admininfo ";
            PreparedStatement pstm = conn.prepareStatement(sql);
            ResultSet rs = pstm.executeQuery();
            rs.next();
            total = rs.getInt(1);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return total;
    }

    /**
     * 增加
     * @param a
     * @return
     */
    @Override
    public int save(Admininfo a) {
        int row=0; //返回执行结果
        try {
            Connection conn= DBUtil.getConn(); //获取连接
            String sql=" insert into admininfo(adminid,username,password,picurl) values(?,?,?,?) "; //定义sql语句
            PreparedStatement pstm=conn.prepareStatement(sql); //预编译sql语句
            pstm.setInt(1,a.getAdminid()); //绑定第一个参数
            pstm.setString(2,a.getUsername());//绑定第二个参数
            pstm.setString(3,a.getPassword());//绑定第三个参数
            pstm.setString(4,a.getPicurl());//绑定第四个参数
            row=pstm.executeUpdate(); //获得执行结果
            pstm.close();
            conn.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

    /**
     * 修改
     * @param a
     * @return
     */
    @Override
    public int update(Admininfo a) {
        int row=0;
        try {
            Connection conn=DBUtil.getConn();
            String sql="update admininfo set username=? ,password=?,picurl=? where adminid=? ";
            PreparedStatement pstm=conn.prepareStatement(sql);
            pstm.setInt(4,a.getAdminid()); //绑定第一个参数
            pstm.setString(1,a.getUsername());//绑定第二个参数
            pstm.setString(2,a.getPassword());//绑定第三个参数
            pstm.setString(3,a.getPicurl());//绑定第四个参数
            row=pstm.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  row;
    }

    /**
     * 删除
     * @param adminid
     * @return
     */
    @Override
    public int delete(int adminid) {
        int row=0;
        try {
            Connection conn=DBUtil.getConn();
            String sql=" delete from admininfo where adminid=? ";
            PreparedStatement pstm=conn.prepareStatement(sql);
            pstm.setInt(1, adminid );
            row=pstm.executeUpdate();
            pstm.close();
            conn.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  row;
    }

    @Override
    public List<Admininfo> getAll() {

        List<Admininfo> adminList=new ArrayList<Admininfo>();
        try {
            Connection conn=DBUtil.getConn();
            String sql="select * from admininfo ";
            PreparedStatement pstm=conn.prepareStatement(sql);
            ResultSet rs=pstm.executeQuery();
            while(rs.next()){
                int adminid=rs.getInt("adminid");
                String username=rs.getString("username");
                String password=rs.getString("password");
                String picurl=rs.getString("picurl");
                Admininfo a=new Admininfo(adminid,username,password,picurl);//使用全参构造函数
                adminList.add(a); //放入集合中
            }
            rs.close();
            pstm.close();
            conn.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

return adminList;
    }

    /**
     * 按Id查找
     * @param adminid
     * @return
     */
    @Override
    public List<Admininfo> get(int adminid) {
        List<Admininfo> adminList0=new ArrayList<Admininfo>();
        try {
            Connection conn = DBUtil.getConn();
            String sql = "select * from admininfo where adminid=? ";
            PreparedStatement pstm = conn.prepareStatement(sql);
            pstm.setInt(1, adminid);
            ResultSet rs = pstm.executeQuery();//执行查询，获取返回结果集
            while (rs.next()) {
                int adminid0= rs.getInt("adminid");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String picurl = rs.getString("picurl");
                Admininfo a=new Admininfo(adminid,username,password,picurl);//使用全参构造函数
                adminList0.add(a); //放入集合中
            }
            rs.close();pstm.close();conn.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return adminList0;
    }
/**
 * 分页查找
 */

    @Override
    public List<Admininfo> getpage(int pageNum, int pageSize) {
        List<Admininfo> admininfoList = new ArrayList<Admininfo>();
        try {
            Connection conn = DBUtil.getConn();
            String sql = "select * from admininfo limit ?,? ";
            PreparedStatement pstm = conn.prepareStatement(sql);
            int start = (pageNum - 1) * pageSize;
            pstm.setInt(1, start);
            pstm.setInt(2, pageSize);
            ResultSet rs = pstm.executeQuery();
            while (rs.next()) {
                int adminid = rs.getInt("adminid");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String picurl = rs.getString("picurl");
                Admininfo p = new Admininfo(adminid, username, password,picurl);
                admininfoList.add(p);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admininfoList;
    }

}
